//
//  DHLeftNotLogView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/21.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHLeftNotLogView.h"
#import "DHSettingViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
#import "DHLoginViewController.h"

@implementation DHLeftNotLogView

+ (DHLeftNotLogView *)shareDHLeftNotLogView{
    DHLeftNotLogView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHLeftNotLogView class]) owner:self options:nil] objectAtIndex:0];
    view.frame = CGRectMake(-kScreenWidth/3 * 2, 0, kScreenWidth/3 * 2, kScreenHeight);
    return view;
}

- (IBAction)settingAction:(id)sender {
    DHSettingViewController *settingController = [[DHSettingViewController alloc] init];
    [self.viewController.navigationController pushViewController:settingController animated:YES];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //判断安装微信
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat]){
        self.wxButton.hidden = NO;
        self.buttonHeightLayout.constant = 100;
    } else {
        self.wxButton.hidden = YES;
        self.buttonHeightLayout.constant = 45;
    }
}

- (IBAction)wxAction:(id)sender {
    [NSObject showHud];
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error){
               if (state == SSDKResponseStateSuccess){
                   NSLog(@"uid=%@",user.uid);
                   NSLog(@"%@",user.credential);
                   NSLog(@"token=%@",user.credential.token);
                   NSLog(@"nickname=%@",user.nickname);
                   [[DHNetAPIClient shareDHNetAPIClient] request_wechat_loginWithParams:@{@"access_token":user.credential.token,@"open_id":user.credential.uid} andBlock:^(id data, NSError *error) {
                       [NSObject hideHUDQuery];
                       if (data) {
                           User *user = [User modelWithJSON:[data objectForKey:@"user"]];
                           user.status = [NSNumber numberWithInt:1];
                           user.token = [data objectForKey:@"token"];
                           [Login doLogin:user];
                           [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                               
                           }];
                           [[GlobalManager shareGlobalManager] refreshVIPInfoAndBlock:^(id data, NSError *error) {
                               
                           }];
                           if ([Login isLogin]) {
                               NSString *sexString = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
                               if (sexString) {
                                   [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":sexString} andBlock:^(id data, NSError *error) {
                                       [kAppDelegate configHomeController];
                                   }];
                               }else{
                                   [kAppDelegate configHomeController];
                               }
                           }
                       }
                   }];
               }else{
                   NSLog(@"%@",error);
                   [NSObject hideHUDQuery];
                   [NSObject showHudTipStr:@"微信授权失败"];
               }
           }];
}

- (IBAction)phoneAction:(id)sender {
    DHLoginViewController *loginController = [[DHLoginViewController alloc] init];
    kAppDelegate.window.rootViewController = loginController;
}

@end
